#include <debug.h>
#include <sys/types.h>

#define COLUMNS                 80
#define LINES                   24
#define VIDEO                   0xB8000

int Debug::xpos = 0;
int Debug::ypos = LINES;


void Debug::dprint(char *cp) {
	if ( !debug() ) { 
		return;
	}
	xpos = 0;
	unsigned short *videomem = (unsigned short*) VIDEO;
	char *str = cp;
    char *ch;
	for(ch = str; *ch; ch++) {
		videomem[xpos+ypos*COLUMNS] =  (unsigned char) *ch | 0x0700 ;
		xpos++;
	}
	ypos--;
	if ( ypos < 0)
		ypos = LINES;
}

void Debug::dprint (long n, int base) {
	char buf[20];
	for(int i = 0; i < 20; i++) {
		buf[i] = ' ';
	}
	
	char *p = buf;
	uint_t ud = n;
	int div = 10;
	
	// decimal conversion
	if (base == 'd' && n < 0) {
		*p++ = '-';
		ud = -ud;
	} else if (base == 'x') {
	    div = 16;
        *p++ = 'h';
	}
	
	do {
		int remainder = ud % div;
		*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
	} while (ud /= div);

	/* Reverse BUF. */
	char* p1 = buf;
	char* p2 = p - 1;
	while (p1 < p2) {
		char tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2--;
	}
	
	dprint(buf);
}
